<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文墨社区-搜索</title>
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" href="css/search.css">
    <script src="js/jquery-3.3.1.js"></script>
    <script src="js/getParameter.js"></script>
    <script>
        $(function () {
            // var search = location.search;
            // //切割字符串，拿到第二个值
            // var cid = search.split("=")[1];
            //当页面加载完成后，调用load方法，发送ajax请求，请求article/pageQuery,传递cid
            var cid = getParameter("cid");
            var rname = getParameter("rname");
            //判断rname不为null或者""
            if (rname) {
                //url解码
                rname = window.decodeURIComponent(rname);
            }
            load(cid);
        });

        function load(cid, currentPage) {
            $.get("article/pageQuery", {cid: cid, currentPage: currentPage}, function (pb) {
                //解析PageBean数据，展示到页面上
                var article_lis = "";
                for (let i = 0; i < pb.list.length; i++) {
                    var article = pb.list[i];
                    var li = '<ul id="article">\n' +
                        '                    <li>\n' +
                        '                        <div>\n' +
                        '                            <div class="user_img">\n' +
                        '\n' +
                        '                            </div>\n' +
                        '                            <div class="user_nickname">\n' +
                        '                                <span>昵称</span>\n' +
                        '                            </div>\n' +
                        '\n' +
                        '                            <div class="article_title">\n' +
                        '                                <span>'+ article.title+'</span>\n' +
                        '                            </div>\n' +
                        '\n' +
                        '                            <div class="article_text">\n' +
                        '                                <span>'+ article.text+'</span>\n' +
                        '                            </div>\n' +
                        '                        </div>\n' +
                        '\n' +
                        '\n' +
                        '                        <div class="article_browserNum">\n' +
                        '                            <div class="article_browserNum_img"></div>\n' +
                        '                            <p>88</p>\n' +
                        '                        </div>\n' +
                        '                        <div class="article_favour">\n' +
                        '                            <div class="article_favour_img"></div>\n' +
                        '                            <p>99</p>\n' +
                        '                        </div>\n' +
                        '                        <div class="article_comment_num">\n' +
                        '                            <div class="article_comment_num_img"></div>\n' +
                        '                            <p>100</p>\n' +
                        '                        </div>\n' +
                        '                    </li>\n' +
                        '                </ul><br><br><br><br><br><br>';
                    article_lis += li;
                }
                $("#article").html(article_lis);

                //分页工具条数据展示
                $("#totalPage").html(pb.totalPage);
                $("#totalCount").html(pb.totalCount);

                //展示分页页码
                var lis = "";
                var firstPage = ' <li style=" cursor:pointer" onclick="load(' + cid + ',' + 1 + ')"><a href="javascript:void(0)">首页</a></li>';
                var beforeNum = pb.currentPage - 1;
                if (beforeNum <= 0) {
                    beforeNum = pb.totalPage;
                }
                var beforePage = '<li style=" cursor:pointer" onclick="load(' + cid + ',' + beforeNum + ')"class="threeword"><a href="javascript:void(0)">上一页</a></li>';
                lis += firstPage;
                lis += beforePage;

                //类似百度的分页，前五后四
                var begin;
                var end;
                if (pb.totalPage < 10) {
                    begin = 1;
                    end = pb.totalPage;
                } else {
                    begin = pb.currentPage - 5;
                    end = pb.currentPage + 4;
                    if (begin <= 0) {
                        begin = 1;
                        end = begin + 9;
                    }
                    if (end > pb.totalPage) {
                        end = pb.totalPage;
                        begin = end - 9;
                    }
                }
                for (let i = begin; i <= end; i++) {
                    var li
                    //判断是否为当前页，如果是加上颜色样式
                    if (pb.currentPage == i) {
                        li = '<li style=" cursor:pointer" class="curPage" onclick="load(' + cid + ',' + i + ')"><a style="display-block:none" href="javascript:void(0)">' + i + '</a></li>';
                    } else {
                        li = '<li style=" cursor:pointer" onclick="load(' + cid + ',' + i + ')"><a style="display-block:none" href="javascript:void(0)">' + i + '</a></li>';
                    }
                    lis += li;
                }


                var afterNum = pb.currentPage + 1;
                if (afterNum >= pb.totalPage) {
                    afterNum = 1;
                }
                var nextPage = '<li style=" cursor:pointer" onclick="load(' + cid + ',' + afterNum + ')"class="threeword"><a href="javascript:void(0)">下一页</a></li>';
                var lastPage = '<li style=" cursor:pointer" onclick="load(' + cid + ',' + pb.totalPage + ')"class="threeword"><a href="javascript:void(0)">尾页</a></li>';
                lis += nextPage;
                lis += lastPage;
                $("#pageNum").html(lis);

                //每次点击都滚动到页面顶部
                // window.scroll(50, 50); //参数为坐标
            });
        }

        //查询分类数据
        $(function () {
            $.get("category/findAll", {}, function (data) {

                var lis = '<li class="nav-active"><a href="category_list.html">分类</a></li>';
                // //遍历请求回来的json数组，拼接字符串
                for (let i = 0; i < data.length; i++) {
                    var li = '<li><a href="category_list.html?cid=' + data[i].cid + '">' + data[i].cname + '</a></li>';
                    lis += li;
                }
                lis += '<li><a href="index.html">返回首页</a></li>';
                //将lis字符串设置到ul的html内容中
                $("#category").html(lis);
            });
        });
    </script>
</head>
<body>
<!-- 首页导航 -->
<div class="navitem">
    <ul id="category" class="nav">


    </ul>
</div>
<!--引入头部-->
<!--<div id="header"></div>-->
<style>
    #article {
        background-color: pink;
        width: 1180px;
        height: 250px;
    }

    #article li {
        height: 250px;
    }

    .user_img {
        width: 50px;
        height: 50px;
        float: left;
        margin-left: 10px;
        margin-top: 10px;
        border: solid 0.05rem #34b3de;
        border-radius: 50%;
        overflow: hidden;
        background-size: cover;
        background-position: 50%;
        background-color: pink;
        background-image: url("images/user5.jpg");
    }

    .user_nickname {
        width: 400px;
        height: 50px;
        background-color: #2aabd2;
        float: left;
        margin-left: 10px;
        margin-top: 10px;
    }
    .user_nickname span{
        line-height: 50px;
    }
    .article_title{
        width: 400px;
        height: 30px;
        background-color: gold;
        float: left;
        margin-left: -400px;
        margin-top: 65px;
    }

    .article_title span{
        line-height: 30px;
    }
    .article_text {
        background-color: #4cae4c;
        width: 1000px;
        height: 100px;
        float: left;
        margin-left: 10px;
        margin-top: 40px;
    }

    .article_browserNum {
        width: 80px;
        height: 40px;
        background-size: cover;
        float: left;
        margin-top: 145px;
        margin-left: -1000px;
    }
    .article_browserNum_img{
        width: 40px;
        height: 40px;
        background-size: cover;
        float: left;
        background-image: url("images/browser_num.png");
        border-radius: 7px;
    }
    .article_browserNum p{
        line-height: 40px;
        text-align: center;
        font-size: 20px;
    }
    .article_favour{
        width: 80px;
        height: 40px;
        float: left;
        margin-top: 145px;
        margin-left: -800px;
    }
    .article_favour_img{
        width: 40px;
        height: 40px;
        background-size: cover;
        float: left;
        background-image: url("images/favour_1.png");
        border-radius: 7px;

    }
    .article_favour p{
        line-height: 40px;
        text-align: center;
        font-size: 20px;
    }
    .article_comment_num{
        width: 80px;
        height: 40px;
        float: left;
        margin-top: 145px;
        margin-left: -600px;
    }
    .article_comment_num_img{
        width: 40px;
        height: 40px;
        background-size: cover;
        float: left;
        background-image: url("images/comment.png");
        border-radius: 7px;

    }
    .article_comment_num p{
        line-height: 40px;
        text-align: center;
        font-size: 20px;
    }
    .article_createTime{
        width: 100px;
        height: 50px;
        margin-top: 10px;
        float: right;
        line-height: 50px;
        background-color: #8a6d3b;
    }
</style>
<div class="page_one">
    <div class="contant">
        <div class="crumbs">
            <img src="images/search.png" alt="">
            <p>文墨社区><span>搜索结果</span></p>
        </div>
        <div class="xinxi clearfix">
            <div class="left">
                <div class="header">
                    <span>文章信息</span>
                </div>
                <ul id="article">
                    <li>
                        <div>
                            <div class="user_img">

                            </div>
                            <div class="user_nickname">
                                <span>昵称</span>
                            </div>

                            <div class="article_createTime">
                                <span>时间</span>
                            </div>

                            <div class="article_title">
                                <span>标题</span>
                            </div>

                            <div class="article_text">
                                <span>正文</span>
                            </div>
                        </div>


                        <div class="article_browserNum">
                            <div class="article_browserNum_img"></div>
                            <p>88</p>
                        </div>
                        <div class="article_favour">
                            <div class="article_favour_img"></div>
                            <p>99</p>
                        </div>
                        <div class="article_comment_num">
                            <div class="article_comment_num_img"></div>
                            <p>100</p>
                        </div>
                    </li>
                </ul>
                <div class="page_num_inf">
                    <i></i> 共
                    <span id="totalPage">12</span>页<span id="totalCount">132</span>条
                </div>
                <div class="pageNum">
                    <ul id="pageNum">
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<!--引入头部-->
<!--<div id="footer"></div>-->
<!--&lt;!&ndash;导入布局js，共享header和footer&ndash;&gt;-->
<!--<script type="text/javascript" src="js/include.js"></script>-->
</body>

</html>